package com.zbkj.admin.controller;

import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.StoreGiftRequest;
import com.zbkj.common.request.StoreGiftSearchRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.StoreGiftResponse;
import com.zbkj.common.response.StoreProductInfoResponse;
import com.zbkj.service.service.StoreGiftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 礼品卡商品配置控制器
 *
 * @author Swallows
 * @date 2022-09-28 14:57
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("api/admin/store/gift")
@Api(tags = "礼品卡——商品")
public class StoreGiftController {

    private final StoreGiftService storeGiftService;

    @PreAuthorize("hasAuthority('admin:gift:list')")
    @ApiOperation(value = "分页列表")
    @GetMapping("/list")
    public CommonResult<CommonPage<StoreGiftResponse>> getList(@Validated StoreGiftSearchRequest request, @Validated PageParamRequest pageParamRequest) {
        CommonPage<StoreGiftResponse> storeGiftResponseCommonPage =
                CommonPage.restPage(storeGiftService.getList(request, pageParamRequest));
        return CommonResult.success(storeGiftResponseCommonPage);
    }

    /**
     * 新增礼品卡可兑换商品
     * @param request 新增参数
     */
    @PreAuthorize("hasAuthority('admin:gift:save')")
    @ApiOperation(value = "新增礼品卡可兑换商品")
    @PostMapping("/save")
    public CommonResult<String> save(@RequestBody @Validated StoreGiftRequest request){
        if(storeGiftService.addGift(request)){
            return CommonResult.success("新增礼品卡可兑换商品成功");
        }else{
            return CommonResult.failed("新增礼品卡可兑换商品失败");
        }
    }

    /**
     * 删除礼品卡可兑换商品
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:gift:delete')")
    @ApiOperation(value = "删除礼品卡可兑换商品")
    @GetMapping("/delete")
    public CommonResult<String> delete(@RequestParam(value = "id") Integer id){
        if(storeGiftService.deleteById(id)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 修改礼品卡可兑换商品
     * @param request 修改参数
     */
    @PreAuthorize("hasAuthority('admin:gift:update')")
    @ApiOperation(value = "修改礼品卡可兑换商品")
    @PostMapping("/update")
    public CommonResult<String> update(@RequestBody @Validated StoreGiftRequest request){
        if(storeGiftService.updateGift(request)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }

    /**
     * 查询礼品卡可兑换商品信息
     * @param id Integer
     */
    @PreAuthorize("hasAuthority('admin:gift:info')")
    @ApiOperation(value = "礼品卡可兑换商品详情")
    @GetMapping("/info")
    public CommonResult<StoreProductInfoResponse> info(@RequestParam(value = "id") Integer id){
        StoreProductInfoResponse detail = storeGiftService.getAdminDetail(id);
        return CommonResult.success(detail);
    }

    /**
     * 修改礼品卡可兑换商品状态
     */
    @PreAuthorize("hasAuthority('admin:gift:update:status')")
    @ApiOperation(value = "修改礼品卡可兑换商品状态")
    @PostMapping("/update/status")
    public CommonResult<Object> updateStatus(@RequestParam(value = "id") Integer id, @RequestParam @Validated boolean isShow){
        if(storeGiftService.updateGiftShow(id, isShow)){
            return CommonResult.success();
        }else{
            return CommonResult.failed();
        }
    }
}
